package com.viethp.mt.activities;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.viethp.mt.activities.R;
import com.viethp.mt.common.CommonHelper;
import com.viethp.mt.common.GlobalVariables;
import com.viethp.mt.objects.AcceptApplicantResponse;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AApplicantDetails extends Activity implements OnClickListener{
	
//	private static final String XML_ITEM_ID = "item_id";
//	private static final String XML_USER_ID = "user_id";
	private static final String XML_ACTIVITY_ID = "activity_id";
	private static final String XML_USERNAME = "username";
	private static final String XML_EMAIL = "email";
	private static final String XML_PHONE = "phone";
	private static final String XML_POSTING = "posting";
	private static final String XML_BOUGHT = "bought";
	private static final String XML_PAID = "paid";
	private static final String XML_BIDDING = "bidding";
	private static final String XML_STATUS= "status";
//	private static final String XML_PRICE = "price";
	
	private ProgressDialog progressDialog;
	private Button btn_back, btn_accept;
	private TextView info_name,info_email,info_phone,tv_info_posted,tv_info_bought,tv_info_paid,tv_infor_bidding,tv_post_payment;
	private String activity_id,username,email,phone,posting,bought,paid,bidding,status;
	private LinearLayout payment_status;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_applicant_details);
		Intent extras = getIntent();
//		item_id = extras.getStringExtra(XML_ITEM_ID);
//		user_id = extras.getStringExtra(XML_USER_ID);
		activity_id = extras.getStringExtra(XML_ACTIVITY_ID);
		username = extras.getStringExtra(XML_USERNAME);
		email = extras.getStringExtra(XML_EMAIL);
		phone = extras.getStringExtra(XML_PHONE);
		posting = extras.getStringExtra(XML_POSTING);
		bought = extras.getStringExtra(XML_BOUGHT);
		paid = extras.getStringExtra(XML_PAID);
		bidding = extras.getStringExtra(XML_BIDDING);
		status = extras.getStringExtra(XML_STATUS);
//		price = extras.getStringExtra(XML_PRICE);
		
		initComponents();
		setOnClickListener();
		setInfo();
		checkStatus(Integer.parseInt(status));
	}
	
	public void checkStatus(int status){
		if(status == 1){
			payment_status.setVisibility(View.INVISIBLE);
		} else if(status == 2){
			tv_post_payment.setText("Waiting for payment");
			btn_accept.setVisibility(View.INVISIBLE);
		} else if(status == 3){
			tv_post_payment.setText("Payment successful");
			btn_accept.setVisibility(View.INVISIBLE);
		}
	}
	
	public void setInfo(){
		info_name.setText(username);
		info_email.setText(email);
		info_phone.setText(phone);
		tv_info_posted.setText(posting);
		tv_info_bought.setText("$"+bought);
		tv_info_paid.setText("$"+paid);
		tv_infor_bidding.setText(bidding);
	}
	
	public void initComponents(){
		
		info_name = (TextView) findViewById(R.id.info_name);
		info_email = (TextView) findViewById(R.id.info_email);
		info_phone = (TextView) findViewById(R.id.info_phone);
		tv_info_posted = (TextView) findViewById(R.id.tv_info_posted);
		tv_info_bought = (TextView) findViewById(R.id.tv_info_bought);
		tv_info_paid = (TextView) findViewById(R.id.tv_info_paid);
		tv_infor_bidding = (TextView) findViewById(R.id.tv_infor_bidding);
		tv_post_payment = (TextView) findViewById(R.id.tv_post_payment);
		payment_status = (LinearLayout) findViewById(R.id.payment_status);
		
		progressDialog = new ProgressDialog(AApplicantDetails.this);
		progressDialog.setCancelable(false);
		
		btn_back = (Button) findViewById(R.id.btn_back);
		btn_accept = (Button) findViewById(R.id.btn_accept);
	}
	public void setOnClickListener(){
		btn_back.setOnClickListener(this);
		btn_accept.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			this.finish();
			break;
		case R.id.btn_accept:
			showLogoutConfirm().show();
			break;
		default:
			break;
		}
		
	}
	
	protected Dialog showLogoutConfirm() {
		return new android.app.AlertDialog.Builder(this)
				.setMessage(getString(R.string.accept_confirm))
				.setCancelable(false)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								doAccept();
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(android.content.DialogInterface dialog,
							int whichButton) {
					}
				}).create();
	}
	
	private void doAccept(){
		if(CommonHelper.isNetworkAvailable(this)){
			startCallServiceAccept();
		} else CommonHelper.showWarning(AApplicantDetails.this,
				getString(R.string.netstate));
	}
	
	private void startCallServiceAccept() {
		progressDialog.setMessage(getString(R.string.pleasewait));
		progressDialog.show();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				Message message = new Message();
				message.obj = callServiceAccept();
				handlerAccept.sendMessage(message);
				
			}
		}).start();
	}
	
	private AcceptApplicantResponse callServiceAccept() {

		SoapObject userRequest = new SoapObject(GlobalVariables.WS_NAMESPACE,
				GlobalVariables.WS_METHOD_ACCEPT_APPLICANT);
		userRequest.addProperty("activity_id", activity_id);

		SoapObject request = new SoapObject(GlobalVariables.WS_NAMESPACE,
				GlobalVariables.WS_METHOD_ACCEPT_APPLICANT);
		request.addProperty(GlobalVariables.WS_METHOD_ACCEPT_APPLICANT, userRequest);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(
				GlobalVariables.WS_ENDPOINT,
				GlobalVariables.WS_CONNECTTION_TIME_OUT);
		SoapObject results = null;
		try {
			androidHttpTransport.call(GlobalVariables.WS_NAMESPACE
					+ GlobalVariables.WS_METHOD_ACCEPT_APPLICANT, envelope);
		} catch (Exception e) {
			try {
				androidHttpTransport.call(GlobalVariables.WS_NAMESPACE
						+ GlobalVariables.WS_METHOD_ACCEPT_APPLICANT, envelope);
			} catch (Exception e1) {
				e1.printStackTrace();
				return null;

			}
		}
		try {
			results = (SoapObject) envelope.getResponse();
		} catch (SoapFault ex3) {
			ex3.printStackTrace();
			return null;
		}
		
		return new AcceptApplicantResponse(results);

	}
	
	private Handler handlerAccept = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			progressDialog.dismiss();

			if (msg.obj != null) {
				AcceptApplicantResponse response = (AcceptApplicantResponse) msg.obj;
				if (response.getErrorCode() != null) {
					CommonHelper.showWarning(AApplicantDetails.this, response
							.getErrorCode().getErrMsg());
				} else {
					status = String.valueOf(response.getStatus());
					checkStatus(Integer.parseInt(status));
					CommonHelper.showWarning(AApplicantDetails.this, "Accept successful");
				}

			} else {
				CommonHelper.showWarning(AApplicantDetails.this,
						getString(R.string.disconnect));
			}

		};
	};
}
